#--coding:utf-8--
# @Time    : 2022/5/13 17:35
# @Author  : hakold
# @File    : playerhome_model.py
# @PROJECT : chatroom_game
import json
import pymongo
import time
from model import player_model, explevel_model

global exp_field_config
exp_field_config = json.load(open("config/exp_field_config.json", encoding='utf-8'))

def mongo_connect():
    #myclient = pymongo.MongoClient('mongodb+srv://hakold:52Beebee@cluster0.gy1rb.mongodb.net/xiuxian?retryWrites=true&w=majority/')
    myclient = pymongo.MongoClient('mongodb://127.0.0.1:27017/')
    #dblist = myclient.list_database_names()
    # dblist = myclient.database_names() 
    mydb = myclient["qt_game"] #创建数据库
    #collist = mydb.list_collection_names()
    mycol = mydb["player"] #集合
    return mycol

# 获取经验信息
def check(data_dict):  
    # inner_type = data_dict["inner_type"].strip()
    data = {}
    account = data_dict['account']
    mycol = mongo_connect()
    check_query = { "account" : account }
    check_doc = mycol.find(check_query)
    check_list = []
    for x in check_doc:
        check_list.append(x)
    player_data = check_list[0]
    del player_data["_id"]
    code = "200"
    data["account"] = player_data["account"]
    data["type"] = "playerhome_renew"
    data["code"] = code
    data["data"] = player_data
    data = json.dumps(data)
    return data

# 吐纳方法
def check_exercise(self, data_dict):
    data = {}
    account = data_dict['account']
    mycol = mongo_connect()
    check_query = { "account" : account }
    check_doc = mycol.find(check_query)
    check_list = []
    for x in check_doc:
        check_list.append(x)
    player_data = check_list[0]
    del player_data["_id"]
    # 检查时间
    last_exercise_time = player_data['home']['last_exercise_time']
    now_time = time.time()
    if last_exercise_time == "":
        # 初始化状态，可以吐纳
        do_exercise(mycol, player_data['account'])
        data['code'] = "200"
    elif int(last_exercise_time) + 10 <= int(now_time):
        # 检查时间通过
        do_exercise(self, mycol, player_data['account'])
        data['code'] = "200"
    else:
        # 不可以
        data['code'] = "500"
    
    data["account"] = player_data["account"]
    data["type"] = "player_homepage_do_exercise"
    data["data"] = player_data
    data = json.dumps(data)
    return data

# 实际吐纳方法
def do_exercise(self, mycol, account):
    account = account
    check_query = { "account" : account }
    check_doc = mycol.find(check_query)
    check_list = []
    for x in check_doc:
        check_list.append(x)
    player_data = check_list[0]
    del player_data["_id"]
    field_level = player_data['home']['gather_exp_field']['level'] # 聚灵阵等级
    # 根据等级找1次吐纳给的经验
    exp_value = exp_field_config[str(field_level)]['do_exercise']
    # 计算时间
    now_time = time.time()
    player_data['home']['last_exercise_time'] = now_time
    player_model.player_attribute_change(self, player_data['account'], 'exp', int(exp_value))
    newvalues = { "$set": 
        { 'home': player_data['home'] },
    }
    mycol.update_one({ "account" : player_data['account'] }, newvalues)
    
# 玩家等级突破
def level_up(self, data_dict):
    data = {}
    if explevel_model.level_up(self, data_dict['account']):
        data["code"] = "200"
    else:
        data["code"] = "500"
        data['message'] = "你的灵力不足！突破失败！"
    
    data["account"] = data_dict["account"]
    data["type"] = "player_homepage_level_up"
    data = json.dumps(data)
    return data